<?php

namespace App\Http\Controllers;

use App\Machine;
use App\Worker;
use Illuminate\Http\Request;
use App\Rule;

class WorkerController extends Controller
{

    public function __construct() {
        $this->middleware('auth');
    }


    public function getAll() {
       $workers=Worker::all();
        return response()->json($workers);
    }
    public function getCurrent($page) {
        $pages=$page*20-20;
        $workers=Worker::skip($pages)->take(20)->get();
        return response()->json($workers);
    }
    public function one($id) {
        $workers=Worker::find($id);
        return response()->json($workers);
    }

    public function getCount() {
        $workers=Worker::all()->count();
        return response()->json($workers);
    }
    public function destroy($id)
    {
        Worker::destroy($id);
        return response()->json('删除成功');
    }



    public function search($word,$type)
    {
        $workers=Worker::where($type,$word)->get();
        return response()->json($workers);
    }

    public function add(Request $request)
    {
        $addWorker=$request->except('_token');
//        $addWorker['machineId']=$request->input('machineId');
//        $machine=Machine::find($addWorker['machineId']);
//        $addWorker['machineName']=$machine->machineName;
        Worker::create($addWorker);
        return response()->json('添加成功');
    }



    public function getOneCode($code)
    {
        $workers=Worker::where('workerCode',$code)->first();
        return response()->json($workers);
    }

    public function update(Request $request, $id)
    {
        $updateWorker=$request->except('_token');
//        $updateWorker['machineId']=$request->input('machineId');
//        $machine=Machine::find($updateWorker['machineId']);
//        $updateWorker['machineName']=$machine->machineName;

        Worker::where('id',$id)->update($updateWorker);
        return response()->json('修改成功');
    }




}
